﻿@using ChiakiYu.Model.Permissions
@model List<Navigation>
@{
    var active = Convert.ToString(ViewData["active"]);
    var permissions = ViewData.Get<IEnumerable<Permission>>("Permissions", null);
}
<ul class="sidebar-menu">
    @foreach (var item in Model.Where(n => n.Level == 1))
    {
        if (item.Controller != "Admin" && permissions != null)
        {
            if (permissions.Where(n => n.Controller == item.Controller) == null || !permissions.Any(n => n.Controller == item.Controller))
            {
                continue;
            }
        }
        if (Model.All(n => n.ParentId != item.Id))
        {
            <li class="@(active == item.Name ? "active" : "") treeview">
                <a href="@item.Url">
                    @if (!string.IsNullOrWhiteSpace(item.IconName))
                    {
                        <i class="@item.IconName"></i>
                    }<span>@item.Name</span>
                </a>
            </li>
        }
        else
        {
            var isActive = Model.Where(n => n.ParentId == item.Id).Any(n => n.Name == active);
            <li class="@(isActive ? "active" : "") treeview">
                <a href="#">
                    @if (!string.IsNullOrWhiteSpace(item.IconName))
                    {
                        <i class="@item.IconName"></i>
                    }
                    <span>@item.Name</span><i class="fa fa-angle-left pull-right"></i>
                </a>
                <ul class="treeview-menu">
                    @foreach (var subItem in Model.Where(n => n.ParentId == item.Id && n.Level != 1))
                    {
                        if (permissions.Where(n => n.Controller == subItem.Controller && n.Action == subItem.Action) == null || !permissions.Any(n => n.Controller == subItem.Controller && n.Action == subItem.Action))
                        { continue; }
                        <li class="@(active == subItem.Name ? "active" : "")">
                            <a href="@subItem.Url">
                                @if (!string.IsNullOrWhiteSpace(subItem.IconName))
                                {
                                    <i class="@subItem.IconName"></i>
                                }
                                @subItem.Name
                            </a>
                        </li>
                    }
                </ul>
            </li>
        }
    }


    @*<li class="treeview">
            <a href="#">
                <i class="fa fa-share"></i> <span>Multilevel</span>
                <i class="fa fa-angle-left pull-right"></i>
            </a>
            <ul class="treeview-menu">
                <li><a href="#"><i class="fa fa-circle-o"></i> Level One</a></li>
                <li>
                    <a href="#"><i class="fa fa-circle-o"></i> Level One <i class="fa fa-angle-left pull-right"></i></a>
                    <ul class="treeview-menu">
                        <li><a href="#"><i class="fa fa-circle-o"></i> Level Two</a></li>
                        <li>
                            <a href="#"><i class="fa fa-circle-o"></i> Level Two <i class="fa fa-angle-left pull-right"></i></a>
                            <ul class="treeview-menu">
                                <li><a href="#"><i class="fa fa-circle-o"></i> Level Three</a></li>
                                <li><a href="#"><i class="fa fa-circle-o"></i> Level Three</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li><a href="#"><i class="fa fa-circle-o"></i> Level One</a></li>
            </ul>
        </li>*@

</ul>
